new Vue({
    el: '#app',
    data() {
      return {
        form: {
          type: "",
          page: 1,			//页数
          limit: 10			//每页数据条数
        },
        total: 0,
        tableData: [],		//表格数据
      }
    },
    mounted() {
      this.getList(this.form)
    },
    computed: {
      toPercent() {
        return function (point) {
          point = Number(point * 100).toFixed(0);
          return point += "%";
        }
      },
    },
    methods: {
      onSerach() {				//查询方法
        console.log("搜搜")
      },
      handleSizeChange(val) {
        this.form.page = 1;
        this.form.limit = val;
        this.getList(this.form)
      },
      handleCurrentChange(val) {
        this.form.page = val;
        this.getList(this.form)
      },
      handleChildren(row){
        const _this = this
        if (!row.Children.order_financing_paystages.length) {
          console.log(row.Children.order_financing_paystages.length)
          $.ajax({
            url: `${DomainName}/getContractPaystagesID?token=${$.cookie("token")}&Order_Financing_Contract_SerialNumber=${row.Order_Financing_Contract_SerialNumber}`,
            type: 'GET',
            success: function (res) {
              row.Children.order_financing_paystages = res.Data.filter(item=>item.Row_Status != '0')
            },
            dataType: "json"
          });
        }
      },
      getList(data) {     //换页方法
        const _this = this
        $.ajax({
          type: 'POST',
          url: `${DomainName}/getFinancingContractAll?token=${$.cookie('token')}`,
          data,
          complete: function (XMLHttpRequest) {
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = '../../../'+XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          success: function (Result) {
            Result.tableData.forEach(item=>{
              item['Children'] = {order_financing_paystages:[]}
            })
            _this.tableData = Result.tableData
            _this.total = parseInt(Result.limit)
            
          },
          dataType: "json"
        });
      },
      editMember(row) {
        sessionStorage.setItem("order_financing_info", JSON.stringify(row))
        window.location.href = 'Contract.html'
      },
      takeEffect(Order_Financing_Contract_SerialNumber){
        const _this = this
        this.$confirm('此操作后合同将正式生效，确保已获取货权！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.effect(Order_Financing_Contract_SerialNumber,(data)=>{
            $.ajax({
              url: `${DomainName}/getEditAction?token=${$.cookie("token")}`,
              type: 'POST',
              data,
              success:function (res) {
                if (res.Status == 'success') {
                  _this.getList(_this.form);
                  _this.$message({
                    type: 'success',
                    message: '合约已正式生效!'
                  });
                }
              },
              error: function () {
                _this.$message.error("删除失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          });
        }).catch(() => {
          this.$message({
            type: 'warning',
            message: '已取消删除'
          });
        });
      },
      deletMember(Order_Financing_SerialNumber){
        const _this = this;
        this.$prompt('请输入驳回原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^(?!\s*$).+/,
          inputErrorMessage: '原因不能由零个或多个空格组成',
          type: 'warning',
        }).then(({ value }) => {
          _this.delete(Order_Financing_SerialNumber,value)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消！'
          });
        });
      },
      effect(Order_Financing_Contract_SerialNumber,callback){
        $.ajax({
          url: DomainName + '/GoodsInquiryKeyOriginal',
          type: 'POST',
          data:{
            Data:'order_financing_contract',
            Primary_Key:Order_Financing_Contract_SerialNumber,
            Primary_Key_Name:'Order_Financing_Contract_SerialNumber'
          },
          success: function (res) {
            if (res.length == 1&&res[0].Row_Status=='1') {
              res[0].Row_Status = '3'
              res[0].Start_Date = getYMDHMSDate()
              let date = new Date(res[0].Start_Date).getTime() + (parseInt(res[0].Repayment_Date)*3600*24*1000)
              res[0].End_Date = getYMDHMSDate(date)
              callback({ Data: JSON.stringify({ Modify: { order_financing_contract: [res[0]] }})})
            }
          },
          complete: function (XMLHttpRequest) {
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
      },
      delete(Order_Financing_SerialNumber,Reason) {
        const _this = this
        $.ajax({
          url: DomainName + '/GoodsInquiryKeyOriginal',
          type: 'POST',
          data:{
            Data:'order_financing_info',
            Primary_Key:Order_Financing_SerialNumber,
            Primary_Key_Name:'Order_Financing_SerialNumber'
          },
          success: function (res) {
            if (res.length == 1) {
              res[0].Row_Status = '2'
              let obj = {
                Order_Financing_Log_SerialNumber:'',
                Order_Financing_SerialNumber,
                Operate:'2',
                TIME:getYMDHMSDate(),
                Reason
              }
              $.ajax({
                url: `${DomainName}/getEditAction?token=${$.cookie("token")}`,
                type: 'POST',
                data:{ Data: JSON.stringify({ 
                  Modify: { order_financing_info: [res[0]] } ,
                  Add: { order_financing_log:[obj] }
                }) },
                success:function (res) {
                  if (res.Status == 'success') {
                    _this.getList(_this.form);
                    _this.$message({
                      type: 'success',
                      message: '驳回成功!'
                    });
                  }
                },
                error: function () {
                  _this.$message.error("删除失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                  if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                    window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  }
                }
              })
            }
          },
          complete: function (XMLHttpRequest) {
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    },
  })
  